<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable width="70%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" style="height:250px;" label-width="120px" @keyup.enter="submitHandle()">
			
			<div class="divline">
				<el-form-item prop="projectCode" class="divlineleft" label="项目编码">
					<el-input v-model="dataForm.projectCode" :disabled="true"  placeholder="项目编码"></el-input>
				</el-form-item>
				<el-form-item prop="projectName" class="divlineleft" label="项目名称">
					<el-input v-model="dataForm.projectName" placeholder="项目名称"></el-input>
				</el-form-item>
			</div>
			<div class="divline">
				<el-form-item prop="userName" class="divlineleft" label="责任人">
					<el-input v-model="dataForm.userName" placeholder="责任人" @click="addOrUpdateUserHandle()"></el-input>
				</el-form-item>
				<el-form-item prop="customerName" class="divlineleft" label="关联客户">
					<el-input v-model="dataForm.customerName" placeholder="关联客户" @click="addOrUpdateCusHandle()"></el-input>
				</el-form-item>
			</div>
			<div class="divline">
				<el-form-item prop="startTime" class="divlineleft" label="开始时间">
					<el-date-picker class="inputVal"
							v-model="dataForm.startTime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;float: left;height: 30px;"
							type="datetime"
						/>
				</el-form-item>
				<el-form-item prop="endTime" class="divlineleft" label="结束时间">
					<el-date-picker class="inputVal"
							v-model="dataForm.endTime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;float: left;height: 30px;"
							type="datetime"
						/>
				</el-form-item>
			</div>
			<div class="divline">
				<el-form-item prop="remark" class="divlinecenter" label="备注">
					<el-input v-model="dataForm.remark" type="textarea"  placeholder="备注"></el-input>
				</el-form-item>
			</div>
			<div class="divlinepro">
				<el-form-item prop="projectProgress" class="divlinecenter" label="项目进度">
					<el-slider v-model="dataForm.projectProgress" :step="1"/>
				</el-form-item>
			</div>
		</el-form>
		<template #footer>
			<div class="divbtn">
				<el-button @click="visible = false">取消</el-button>
				<el-button  v-auth="'sys:order:save'" type="primary" @click="submitHandle()">确定</el-button>
			</div>
		</template>
		<!-- 弹窗, 新增 / 修改 -->
		<datalistUser ref="datalistUserRef" @fatherMethod="fatherMethod"></datalistUser>
		<!-- 弹窗, 新增 / 修改 -->
		<datalistCus ref="datalistCusRef" @fatherCusMethod="fatherCusMethod"></datalistCus>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import {useProjectSubmitApi,useProjectApi} from '@/api/sys/project'
import datalistUser from './datalist.vue'
import datalistCus from './customer.vue'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	id: '',
	projectCode:'',
	projectName:'',
	projectProgress:'',
	customerId:'',
	customerName:'',
	userId:'',
	userName:'',
	remark:'',
	startTime:'',
	endTime:'',
})
const fatherMethod=(userId:any,userName:any)=>{
	dataForm.userId=userId;
	dataForm.userName=userName;
}
const fatherCusMethod=(customerId:any,customerName:any)=>{
	dataForm.customerId=customerId;
	dataForm.customerName=customerName;
}
const init = (obj:any) => {
	visible.value = true
	dataForm.id = '';
	dataForm.projectCode=getRandNum();
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	// id 存在则为修改
	if (obj) {
		getProject(obj.id);
	}else{
		dataForm.userId="";
		dataForm.userName="";
		dataForm.customerId="";
		dataForm.customerName="";
	}
}
const getRandNum= ()=> {
    const projectTime = new Date() // 当前中国标准时间
    const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
    const Month = projectTime.getMonth() + 1 // 获取中国区月份
    const Day = projectTime.getDate() // 获取几号
    var CurrentDate ="P"+ Year
    if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
        CurrentDate += Month
    } else {
        CurrentDate += '0' + Month
    }
    if (Day >= 10) {
        CurrentDate += Day
    } else {
        CurrentDate += '0' + Day
    }
    return CurrentDate+ Math.floor(Math.random() * 10000);
}
// 获取信息
const getProject = (id: number) => {
	useProjectApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}
const datalistUserRef = ref()
const addOrUpdateUserHandle = () => {
	datalistUserRef.value.init();
}
const datalistCusRef = ref()
const addOrUpdateCusHandle = () => {
	datalistCusRef.value.init();
}
const dataRules = ref({
	projectName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	startTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	endTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	userName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	customerName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useProjectSubmitApi(dataForm).then((res:any) => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>


<style lang="scss" scoped>
.divline {
	width: 96%;
	height: 30px;
	float: left;
	margin-top: 20px;
}
.divlinepro{
	
	width: 96%;
	height: 30px;
	float: left;
	margin-top: 50px;
}
.divfileline{
	width: 96%;
	float: left;
	margin-left: 40px;
	margin-top: 40px;
}
.divlineleft{
	width: 47%;
	float: left;
	height: 30px;
	padding-left: 10px;
}

.divlinecenter{
	width: 95.5%;
	float: left;
	height: 30px;
	padding-left: 10px;
}
.divbtn{
	text-align: center;
	margin-top: 20px;

}
.eduline{
	width: 96%;
	float: left;
	height: 50px;
	margin-top: 15px;
	border-bottom: 1px solid #BEBEBE;
	border-top: 1px solid #BEBEBE;
}
.divsplit{
	width: 96%;
	float: left;
	height: 1px;
	background-color:#409eff;
    margin-top: 5px;
}
.divsplit:last-child{
	width: 96%;
	float: left;
	height: 1px;
}
.eduitem{
	width: 20%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
.inputVal.el-input__wrapper{
	width: 60%;
	float: left;
	height: 30px;
}
.inputVal.el-input{
	width: 60%;
	float: left;
	height: 30px;
}
.texttit{
	width: 30%;
	float: left;
	height: 30px;
	line-height: 2;
}
.eduitembtn{
	
	width: 19%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
</style>
